import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:geomancy/styles/index.dart';
import 'package:geomancy/widgets/common_text.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class CommonListWidget extends StatefulWidget {
  final RefreshController controller;
  final Widget child;
  final bool? enablePullDown;
  final bool? enablePullUp;
  final String? noDataText;
  final Widget? header;
  final VoidCallback? onRefresh;
  final VoidCallback? onLoading;
  const CommonListWidget({Key? key, required this.controller, required this.child, this.onRefresh, this.onLoading, this.enablePullDown, this.enablePullUp, this.noDataText, this.header}) : super(key: key);

  @override
  State<CommonListWidget> createState() => _CommonListWidgetState();
}

class _CommonListWidgetState extends State<CommonListWidget> {

  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
      controller: widget.controller,
      enablePullDown: widget.enablePullDown ?? true,
      enablePullUp: widget.enablePullUp ?? true,
      onRefresh: widget.onRefresh,
      onLoading: widget.onLoading,
      header: widget.header,
      footer: CustomFooter(
        builder: (BuildContext context, LoadStatus? mode) {
          Widget body ;
          if(mode==LoadStatus.idle){
            body = CommonText(txt: '上拉加载', fontSize: 26.sp);
          }
          else if(mode==LoadStatus.loading){
            body = CupertinoActivityIndicator(radius: 20.w, color: ColorStyle.borderColor);
          }
          else if(mode == LoadStatus.failed){
            body = CommonText(txt: '加载失败！点击重试！', fontSize: 26.sp);
          }
          else if(mode == LoadStatus.canLoading){
            body = CommonText(txt: '松手,加载更多!', fontSize: 26.sp);
          }
          else{
            body = CommonText(txt: '没有更多数据了!', fontSize: 26.sp);
          }
          return SizedBox(
            height: 55.0,
            child: Center(child: body),
          );
        },
      ),
      child: widget.child,
    );
  }
}
